﻿using System;
using System.ComponentModel;

namespace XamlMvp
{
    public class PresenterBindingTypeConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return sourceType == typeof(string);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            Type runtimeType = null;
            
            string typeString = value as string;

            if (!string.IsNullOrEmpty(typeString))
            {
                runtimeType = Binders.Presenters.Find(typeString);
            }

            return new PresenterBinding(runtimeType);
        }        
    }
}
